<?php
class Article extends MY_Controller
{
	public function __construct(){
		parent::__construct();
	}
	
	public function index($page=0){
		$data['title'] = '文章管理';
		$per_page = 20;
		
		$n = $this->db->get('article');
		
//		print_r($admins);
		$data['list_current'] = array('article','article');
		$data['js'] = array('article');
		
		
		$this->load->library ( 'pagination' );		
		$config ['base_url'] = site_url('/sysadmin/article/index');
		$config ['total_rows'] = $n->num_rows();
		$config ['per_page'] = $per_page;
		$config ['uri_segment'] = 4;
		$this->pagination->initialize ( $config );
		
		$o = $this->db->query("SELECT a.*, at.title as typename FROM {$this->db->dbprefix}article a 
		LEFT JOIN {$this->db->dbprefix}articletype at
		ON a.typeid = at.id
		ORDER BY a.id DESC
		LIMIT $page, $per_page");
		$values = $o->result();
		$data['values'] = $values;
		
		$data['pageinfo'] =  $this->pagination->create_links ();		
		$this->load->view('admin/header', $data);
		$this->load->view('admin/article', $data);
		$this->load->view('admin/footer');
	}
	function add($id=NULL){
		if($this->input->post('title')){
			$setarr = array(
				'title'=>$this->input->post('title'),
				'typeid'=>intval($this->input->post('typeid')),
				'content'=>$this->input->post('content'),
				'updatetime'=>date("Y-m-d H:i:s"));
			if(!$this->input->post('id')){//添加
				$setarr['createtime'] = $setarr['updatetime'];
				$this->db->insert('article', $setarr);
				echo $this->db->insert_id(); 
				die();
			}else{
				$this->db->where('id', $this->input->post('id'));
				if($this->db->update('article', $setarr)){
					echo 1;
				}else{
					echo -1;
				}
				
			}
		}else{
			if($id){//编辑时
				$a = $this->db->query("select a.*, at.title as pidSel
				from {$this->db->dbprefix}article a
				LEFT JOIN {$this->db->dbprefix}articletype at
				ON a.typeid = at.id
				where a.id=$id");
				echo json_encode($a->result());
			}else{
				echo '-1';
			}
			
		}
	}
	function delete($id=NULL){
		if($id){
			$id = explode('-', $id);
			$this->db->where_in('id', $id);
			if($this->db->delete('article')){
				echo 1;
			}else{
				echo -1;
			}
			
		}
	}
}